package com.woniu.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.woniu.adapter.FundAdapter;
import com.woniu.inlet.fo.FundGraphVo;
import com.woniu.outlet.elasticSearch.po.Fund;
import com.woniu.outlet.mysql.po.FundRela;
import com.woniu.outlet.redis.po.FundRedis;
import com.woniu.service.command.*;
import com.woniu.util.po.NetWorth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.net.URISyntaxException;
import java.util.List;

/**
 * @description:
 * @author: my
 * @time: 2021/4/20 20:25
 */
@Slf4j
@Service
public class FundService implements FundDetailCommandHandler, FundQueryCommandHandler,
        HotFundCommandHandler,FundPositionCommandHandler,FundGraphCommandHandler,
        SearchFundCommandHandler,FundAllCommandHandler,ChangeFundStatusCommandHandler,FundShowCommandHandler {
    private FundAdapter fundAdapter;

    public FundService(FundAdapter fundAdapter) {
        this.fundAdapter = fundAdapter;
    }

    /**
     * 获取基金详情
     *
     * @param fundDetailCommand
     */
    @Override
    public FundRedis action(FundDetailCommand fundDetailCommand) {
        String code = fundDetailCommand.getCode();
        return fundAdapter.getDetailByCode(code);
    }

    /**
     * 根据类型获取基金
     *
     * @param fundQueryCommand
     * @return
     */
    @Override
    public List<Fund> action(FundQueryCommand fundQueryCommand) throws Exception {
        String type = fundQueryCommand.getType();
        Integer pageNum = fundQueryCommand.getPageNum();
        List<Fund> funds = fundAdapter.getfundsByType(type,pageNum);
        return funds;
    }

    @Override
    public List<Fund> action(HotFundCommand hotFundCommand) throws Exception {
        return fundAdapter.getHot();
    }

    /**
     * 获取持仓数据
     * @param fundPositionCommand
     * @return
     * @throws URISyntaxException
     */
    @Override
    public JSONObject action(FundPositionCommand fundPositionCommand) throws URISyntaxException {
        String code = fundPositionCommand.getCode();
        return fundAdapter.getPosition(code);
    }

    /**
     * 基金折线图数据
     * @param fundGraphCommand
     * @return
     * @throws URISyntaxException
     */
    @Override
    public List<NetWorth> action(FundGraphCommand fundGraphCommand) throws URISyntaxException {
        FundGraphVo fundGraphVo = fundGraphCommand.getFundGraphVo();
        return fundAdapter.FundGraph(fundGraphVo.getCode(),
                fundGraphVo.getMonth());
    }

    /**
     * 模糊查询基金
     * @param command
     * @return
     */
    @Override
    public List<Fund> action(SearchFundCommand command) {
        String name = command.getName();
        return fundAdapter.fundSearch(name);
    }


    @Override
    public List<Fund> action(FundAllCommand command) {
        return fundAdapter.getAllFund();
    }

    /**
     * 修改基金状态
     * @param command
     */
    @Override
    public void action(ChangeFundStatusCommand command) {
        Integer id = command.getId();
        fundAdapter.changeFundStatus(id);
    }

    /**
     * 查询所有基金
     * @param command
     * @return
     */
    @Override
    public List<FundRela> action(FundShowCommand command) {
        String status = command.getStatus();
        Integer pageIndex = command.getPageIndex();
        Integer pageNum = command.getPageNum();
        return fundAdapter.queryAllFund(status,pageIndex,pageNum);
    }
}
